function LinkedList() {
    function Node(ele) {
        this.ele = ele
        this.next = null
    }
    this.length = 0
    this.head = null
    // 新增表头
    LinkedList.prototype.append = function (ele) {
        var newNode = new Node(ele)
        // 判断原链表是否为空
        if (this.head === null) {
            // 增加表头
            this.head = newNode
        } else {
            var current = this.head
            // 循环指向下一个节点，直到最后一个节点
            while (current?.next) {
                current = current.next
            }
            // 表尾添加节点
            current.next = newNode
        }
        this.length++
    }
    // 转字符串
    LinkedList.prototype.toString = function () {
        var current = this.head
        var listString = ''
        while(current) {
            listString += "." + current.ele
            current = current.next
        }
        return listString.slice(1)
    }
    // 任意位置插入
    LinkedList.prototype.insert = function (position, ele)  {
        // 检测越界。
        if (position < 0 || position > this.length) return false
        var newNode = new Node(ele)
        var current = this.head
        var previous = null
        var index = 0
        if (position == 0) {
            newNode.next = current
            this.head = newNode
        } else {
            while (index++ < position) {
                previous = current
                current = current.next
            }
            newNode.next = current
            previous.next = newNode
        }
        this.length++
        return true
    }
    // 根据位置删除元素
    LinkedList.prototype.removeAt = function (position) {
        if (position < 0 || position >= this.length) return false
        var current = this.head
        var previous = null
        var index= 0
        if (position == 0) {
            this.head = current.next
        } else {
            while(index++ < position) {
                previous = current
                current = current.next
            }
            previous.next = current.next
        }
        this.length--
        return current
    }
    // 根据元素获取在列表中的位置
    LinkedList.prototype.indexOf = function (ele) {
        var current = this.head
        var index = 0
        while (current) {
            if (current.ele === ele) {
                return index
            }
            index++
            current = current.next
        }
        return -1
    }
    // 根据元素删除
    LinkedList.prototype.remove = function (ele) {
        var index = this.indexOf(ele)
        return this.removeAt(index)
    }
    // 判断链表是否为空
    LinkedList.prototype.isEmpty = function () {
        return this.length == 0
    }
    // 获取链表的长度
    LinkedList.prototype.size = function () {
        return this.length
    }
    // 获取第一个节点
    LinkedList.prototype.getFirst = function () {
        return this.head.element
    }
}
var list = new LinkedList()

// 2.追加元素
list.append(1)
list.append(2)
list.append(3)
list.append(5)

// 3.插入元素
list.insert(3, 4)
// 4.删除元素
console.log(list.removeAt(3));
list.remove(3)
list.remove(5)
// 5.查找元素
// console.log(list.indexOf(3));
// console.log(list.indexOf(4));
// 6.根据元素来删除
console.log(list)